package club.monkeywood.transaction.ticket.controller;


import club.monkeywood.transaction.ticket.dao.TicketRepository;
import club.monkeywood.transaction.ticket.dto.OrderDTO;
import club.monkeywood.transaction.ticket.service.TicketService;
import club.monkeywood.transaction.ticket.vo.Ticket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
@RequestMapping("/api/ticket")
public class TicketController {

    @Autowired
    private TicketRepository ticketRepository;

    @PostConstruct
    public void init() {
        if(ticketRepository.count()>0){
            return;
        }
        Ticket ticket = new Ticket();
        ticket.setName("Num.1");
        ticket.setTicketNum(100L);
        ticketRepository.save(ticket);
    }

    @Autowired
    private TicketService ticketService;

    @PostMapping("/lock")
    public void lock(@RequestBody OrderDTO orderDTO) {
        ticketService.ticketLock(orderDTO);
    }

    @PostMapping("/lock2")
    public void lock2(@RequestBody OrderDTO orderDTO) {
        ticketService.ticketLock2(orderDTO);
    }

}
